#ifndef CONTACTTREE_H
#define CONTACTTREE_H

#include <wx/event.h>
#include <wx/treectrl.h>
#include <wx/artprov.h>
#include <wx/bitmap.h>

#include "tux.h"

class MsnContact;
class MsnMessenger;

class PropertyWindow;

class ContactTreeCtrl : public wxTreeCtrl
{
friend class MainFrame;
friend class MessengerPanel;
friend class ContactPanel;

private:
    MsnMessenger* messenger;
	MessengerPanel* messengerPanel;

	PropertyWindow* propertyWindow;

	wxTreeItemId dragSrcItem;

	wxTreeItemId stuckItem;
	wxPoint stuckPosition;
	time_t stuckTime;
	
public:
	ContactTreeCtrl(){};
    ContactTreeCtrl(wxWindow* parent, MessengerPanel* mp, int id);
	~ContactTreeCtrl();

public:
    void LoadContactData();

	void Clear();

	void ClearPropertyWindow();

	void ShowMe(bool shown);

	void SearchContact(const wxString& str);

    void AddGroupItem(const wxString& groupID, const wxString& groupName);

    void AddContactItem(const wxString& contactID, const wxString& groupID, const wxString& label, MsnContact* contact);

    void AddContactItem(MsnContact* contact);

    void DeleteContactItem(const wxString& contactID);
    void DeleteContactItem(const wxString& contactID, const wxString& groupID);

    wxTreeItemId FindContactItem(const wxString& contactID);
    wxTreeItemId FindContactItem(const wxTreeItemId& parent, const wxString& contactID);

    wxTreeItemId FindContactItem(const wxString& contactID, const wxString& groupID);
    wxTreeItemId FindContactItem(const wxTreeItemId& parent, const wxString& contactID, const wxString& groupID);

    wxTreeItemId FindContactItemByPassport(const wxString& passport);
    wxTreeItemId FindContactItemByPassport(const wxTreeItemId& group, const wxString& passport);

    void UpdateContactItemStatus(const wxString& passport, const wxString& status);

    void UpdateContactItemLabel(const wxString& passport, const wxString& label);
    void UpdateContactItemStatus(wxTreeItemId& item, const wxString& status);


//    wxTreeItemId FindContact(const wxString& passport);
//    wxTreeItemId FindContactByID(const wxString& id);
//    wxTreeItemId FindContact(const wxTreeItemId& parent, const wxString& passport, bool byID);

    wxTreeItemId FindGroupItemByID(const wxString& groupID);
    void DeleteGroupItemByID(const wxString& groupID);
    void UpdateGroupItem(const wxString& groupID, const wxString& groupName);

	void UpdateGroupItem(const wxString& groupID);
	void UpdateGroupItemForContact(MsnContact* contact);

	void CheckMouseMotion();

protected:
    void OnRightClick(wxMouseEvent& event);
    void OnLeftClick(wxMouseEvent& event);
    void OnLeftDClick(wxMouseEvent& event);

	void OnMouseMotion(wxMouseEvent& event);

    void OnSendMessage(wxCommandEvent& event);
	void OnSendFile(wxCommandEvent& event);

    void OnViewHistory(wxCommandEvent& event);

	void OnInviteSession(wxCommandEvent& event);

    void OnAddContact(wxCommandEvent& event);
    void OnMoveContact(wxCommandEvent& event);

	void OnBlockContact(wxCommandEvent& event);

    void OnDeleteContact(wxCommandEvent& event);

    void OnRemoveContactFromGroup(wxCommandEvent& event);


    void OnGroupOpen(wxTreeEvent& event);
    void OnGroupClose(wxTreeEvent& event);

    void OnRenameContact(wxCommandEvent& event);

	void OnCancelNickname(wxCommandEvent& event);

    void OnRenameBegin(wxTreeEvent& event);
    void OnRenameEnd(wxTreeEvent& event);

    void OnDragBegin(wxTreeEvent& event);
    void OnDragEnd(wxTreeEvent& event);

    void OnAddGroup(wxCommandEvent& event);
    void OnRenameGroup(wxCommandEvent& event);
    void OnRemoveGroup(wxCommandEvent& event);

    int OnCompareItems(const wxTreeItemId& item1, const wxTreeItemId& item2);

    DECLARE_EVENT_TABLE();
	DECLARE_DYNAMIC_CLASS(ContactTree)
};


class MsnTreeData : public wxTreeItemData
{
public:
    MsnTreeData(const wxString& _id, const wxString _label):ID(_id),label(_label),parentID(wxEmptyString),contact(NULL){};
    MsnTreeData(const wxString& _id, const wxString _label,const wxString& _pid, MsnContact* _contact)
        :ID(_id),label(_label),parentID(_pid),contact(_contact){};

    bool IsGroup(){return contact == NULL;}
    bool IsContact(){return contact != NULL;}

    wxString GetID(){return ID;}
    wxString GetParentID(){return parentID;}
    wxString GetLabel(){return label;}
    MsnContact* GetContact(){return contact;}

private:
    wxString ID;
    wxString label;
    wxString parentID;

    MsnContact* contact;
};


#endif


